route.test.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* @vitest-environment node */
  2. import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
  3. import fs from "node:fs/promises";
  4. import os from "node:os";
  5. import path from "node:path";
  6. vi.mock("@/lib/auth/session", () => ({
  7. getSession: vi.fn(),
  8. }));
  9. import { getSession } from "@/lib/auth/session";
  10. import { GET } from "./route.js";
  11. describe("GET /api/branches/[branch]/[year]/months", () => {
  12. let tmpRoot;
  13. const originalNasRoot = process.env.NAS_ROOT_PATH;
  14. beforeEach(async () => {
  15. vi.clearAllMocks();
  16. tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "api-months-"));
  17. process.env.NAS_ROOT_PATH = tmpRoot;
  18. await fs.mkdir(path.join(tmpRoot, "NL01", "2024", "10"), {
  19. recursive: true,
  20. });
  21. });
  22. afterEach(async () => {
  23. process.env.NAS_ROOT_PATH = originalNasRoot;
  24. if (tmpRoot) await fs.rm(tmpRoot, { recursive: true, force: true });
  25. });
  26. it("returns 401 when unauthenticated", async () => {
  27. getSession.mockResolvedValue(null);
  28. const res = await GET(
  29. new Request("http://localhost/api/branches/NL01/2024/months"),
  30. {
  31. params: Promise.resolve({ branch: "NL01", year: "2024" }),
  32. }
  33. );
  34. expect(res.status).toBe(401);
  35. expect(await res.json()).toEqual({ error: "Unauthorized" });
  36. });
  37. it("returns 403 when branch user accesses a different branch", async () => {
  38. getSession.mockResolvedValue({
  39. role: "branch",
  40. branchId: "NL01",
  41. userId: "u1",
  42. });
  43. const res = await GET(
  44. new Request("http://localhost/api/branches/NL02/2024/months"),
  45. {
  46. params: Promise.resolve({ branch: "NL02", year: "2024" }),
  47. }
  48. );
  49. expect(res.status).toBe(403);
  50. expect(await res.json()).toEqual({ error: "Forbidden" });
  51. });
  52. it("returns months for a valid branch/year when allowed", async () => {
  53. getSession.mockResolvedValue({
  54. role: "admin",
  55. branchId: null,
  56. userId: "u2",
  57. });
  58. const res = await GET(
  59. new Request("http://localhost/api/branches/NL01/2024/months"),
  60. {
  61. params: Promise.resolve({ branch: "NL01", year: "2024" }),
  62. }
  63. );
  64. expect(res.status).toBe(200);
  65. const body = await res.json();
  66. expect(body).toEqual({ branch: "NL01", year: "2024", months: ["10"] });
  67. });
  68. it("returns 400 when branch or year is missing (authenticated)", async () => {
  69. getSession.mockResolvedValue({
  70. role: "admin",
  71. branchId: null,
  72. userId: "u2",
  73. });
  74. const res = await GET(
  75. new Request("http://localhost/api/branches/NL01//months"),
  76. {
  77. params: Promise.resolve({ branch: "NL01", year: undefined }),
  78. }
  79. );
  80. expect(res.status).toBe(400);
  81. expect(await res.json()).toEqual({ error: "branch oder year fehlt" });
  82. });
  83. });